package com.iyue.util;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author guanshaojie
 * @version 1.0.0
 * @since 2008-12-04
 */
public class UserAgentUtil {

	private static final Pattern UA_PATTERN = Pattern
			.compile("(([a-zA-Z\\d]+)([^a-zA-Z\\d]|[^a-zA-Z\\d]+\\s*)([a-zA-Z\\d]+)([^a-zA-Z\\d]?)([^\\/\\s\\-\\_]*))");

	private static final Pattern SUB_UA_PATTERN = Pattern
			.compile("(([a-zA-Z]+)([\\d])+)");

	private static final Pattern PHILIPS_UA_PATTERN = Pattern
			.compile("(([a-zA-Z\\d]+)(.)([a-zA-Z\\d\\@]+|\\/{1}))");

	private static final Pattern SPECIFIC_UA_PATTERN = Pattern
			.compile("(([a-zA-Z\\d]+)([^a-zA-Z\\d][a-zA-Z]+)([0-9]+)(\\+?))");

	public UserAgentUtil() {
	}

	public static String getUserAgent(String s) {
		StringBuffer stringbuffer = new StringBuffer();
		if (s.startsWith("LG") || s.startsWith("LENOVO")
				|| s.startsWith("KONKA") || s.startsWith("Capitel")) {
			Matcher matcher = SPECIFIC_UA_PATTERN.matcher(s);
			if (matcher.find()) {

				stringbuffer.append(matcher.group());
			}
		} else if (s.startsWith("Mozilla")
				&& (s.indexOf("MOT") != -1 || s.indexOf("Nokia") != -1 || s
						.indexOf("Samsung") != -1)) {

			int end = 0;
			int start = 0;
			if (s.indexOf("MOT") != -1) {
				start = s.indexOf("MOT");
				end = s.indexOf("/", start);
				stringbuffer.append(s.substring(start, end));
			} else if (s.indexOf("Nokia") != -1) {

				/*
				 * start = s.indexOf("Nokia"); end = s.indexOf("/", start);
				 * String temp = s.substring(start, end);
				 */
				/*
				 * if(temp.indexOf(" ")!=-1){ temp =
				 * temp.substring(start,temp.indexOf(" ",start)); }
				 */
				String temp = getUserAgent(s.substring(s.indexOf("Nokia"))
						.replaceAll("/", " "));

				stringbuffer.append(temp);

			} else if (s.indexOf("Samsung") != -1) {
				String temp = getUserAgent(s.substring(s.indexOf("Samsung"))
						.replaceAll("/", " "));

				stringbuffer.append(temp);
			}
		} else if (s.startsWith("Philips") || s.startsWith("CECT")
				|| s.startsWith("TCL") || s.startsWith("HAIER")) {
			Matcher matcher1 = PHILIPS_UA_PATTERN.matcher(s);
			if (matcher1.find()) {
				if (matcher1.group(3).matches("\\/")) {
					stringbuffer.append(matcher1.group(2));
				} else if (matcher1.group(4).matches("\\/")) {
					stringbuffer.append(matcher1.group(2)).append(
							matcher1.group(3));
				} else {
					stringbuffer.append(matcher1.group(2)).append(
							matcher1.group(3)).append(matcher1.group(4));
				}
			}
		} else {

			Matcher matcher2 = UA_PATTERN.matcher(s);
			if (matcher2.find()) {

				/*
				 * System.out.println(matcher2.group(2));
				 * System.out.println(matcher2.group(3));
				 * System.out.println(matcher2.group(4));
				 * System.out.println(matcher2.group(5));
				 */

				stringbuffer.append(matcher2.group(2));

				if (!matcher2.group(3).matches("\\/")
						&& !matcher2.group(3).matches("\\/.+")
						&& (!matcher2.group(2).startsWith("Nokia") || matcher2
								.group(2).startsWith("Nokia")
								&& matcher2.group(4).length() > 3)
						&& !matcher2.group(2).startsWith("SonyEricsson")) {
					if (!matcher2.group(5).matches("\\/")
							&& !matcher2.group(5).matches("\\*")
							&& !matcher2.group(5).matches("\\s")
							&& !matcher2.group(5).matches("-")
							&& !matcher2.group(5).matches("_")
							&& !matcher2.group(5).equals("")
							&& !matcher2.group(5).matches("\\+")
							&& !matcher2.group(5).matches("\\(")) {
						stringbuffer.delete(0, stringbuffer.length()).append(
								matcher2.group());
					} else if (!matcher2.group(3).matches("\\s")) {
						// System.out.println("平淡是福");
						if (matcher2.group(5).matches("\\s")
								&& matcher2.group(6).indexOf(".") == -1
								&& matcher2.group(2).equals("MOT")) {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4))
									.append(matcher2.group(5)).append(
											matcher2.group(6));
						} else if (matcher2.group(5).matches("\\+")) {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4))
									.append(matcher2.group(5));
						} else if (matcher2.group(5).matches("\\-")) {
							Matcher matcher3 = SUB_UA_PATTERN.matcher(matcher2
									.group(6));
							if (matcher3.find()) {
								stringbuffer.append(matcher2.group(3)).append(
										matcher2.group(4)).append(
										matcher2.group(5)).append(
										matcher3.group());
							} else {
								stringbuffer.append(matcher2.group(3)).append(
										matcher2.group(4)).append(
										matcher2.group(5)).append(
										matcher2.group(6));
							}
						} else if (matcher2.group(3).matches("\\/++\\s*")) {

						} else if (matcher2.group(5).matches("\\/")
								&& matcher2.group(4)
										.matches("[cC][mM][cC][cC]")) {

						} else if (matcher2.group(2).matches("TIANYU")
								&& matcher2.group(3).matches("-")
								&& matcher2.group(5).matches("\\/")) {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4)).append("-").append(
									matcher2.group(6));
						}

						else {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4));
						}
					} else if (matcher2.group(5).matches("\\-")) {

						stringbuffer.append(matcher2.group(3)).append(
								matcher2.group(4)).append(matcher2.group(5))
								.append(matcher2.group(6));
					} else if (matcher2.group(5).matches("\\s")) {
						if (matcher2.group(2).matches("TIANYU")) {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4));
						} else if (matcher2.group(2).matches("Nokia")) {
							stringbuffer.append(matcher2.group(4));
						} else if (matcher2.group(2).matches("Samsung")) {
							stringbuffer.append(matcher2.group(4));
						} else if (matcher2.group(2).startsWith("SamsungSGH")) {

						} else {
							stringbuffer.append(matcher2.group(3)).append(
									matcher2.group(4))
									.append(matcher2.group(5)).append(
											matcher2.group(6));
						}
					} else if (matcher2.group(3).matches("\\s")
							&& matcher2.group(2).startsWith("Nokia")) {
						/*
						 * System.out.println("stringbuffer is "+stringbuffer);
						 * stringbuffer.append(matcher2.group(2));
						 */
					} else if (((matcher2.group(2).startsWith("Dopod") || matcher2
							.group(2).startsWith("PHILIPS")) && matcher2.group(
							3).matches("\\s+"))) {

					} else {

						stringbuffer.append(matcher2.group(3)).append(
								matcher2.group(4));
					}
				} else if (matcher2.group(3).matches("\\/")
						&& matcher2.group(2).matches("Typhoon")) {
					stringbuffer.append(matcher2.group(3)).append(
							matcher2.group(4));
				} else if (matcher2.group(3).matches("\\s")
						&& matcher2.group(2).matches("Nokia")) {
					stringbuffer.append(matcher2.group(4));
				} else if (matcher2.group(2).matches("Samsung")
						&& matcher2.group(3).matches("\\/")) {
					s = s.replaceAll("\\/", "-");
					return getUserAgent(s);
				} else if (matcher2.group(2).matches("PalmCentro")
						&& matcher2.group(3).matches("\\/")) {
					stringbuffer.append("-").append(matcher2.group(4));
				}
			} else if (s.indexOf("/") == -1) {

				stringbuffer.append(s);
			} else {

				stringbuffer.append(s.substring(0, s.indexOf("/")));
			}
		}
		// System.out.println(stringbuffer.toString());

		return stringbuffer.toString();
	}

	public static void main(String args[]) throws IOException {

		System.out
				.println(getUserAgent("Nokia5320_CMCC/04.13 (SymbianOS/9.3; U;Series60/3.2 Mozilla/5.0; Profile/MIDP-2.1 Configuration/CLDC-1.1 )AppleWebKit/413 (KHTML, like Gecko) Safari/413"));
		System.out
				.println(getUserAgent("Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN76-1/30.0.015 Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413"));
	}

}
